<template>
  <!-- 大客户线索管理->合同备案查询 -->
  <div>
    <!-- 搜索 -->
    <TopFilter
      ref="topFilterRef"
      :list="topFilterInit"
      :cols="3"
      labelWidth="100"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      ></TopFilter>
     <!-- table -->
    <FilterTable   ref="table" :columns="columns"
       :fetchapi="fetchApi"
       :isSelectColumn="isSelectColumn"
       :params="fetchParams"
       :datakey="'records'"
       columnsRef='customerManage_filingManage_contractRegister_index_01'
       :onColumnsChange="columns => this.columns = columns">
      <template slot="controls" >
        <el-button size="small" type="primary" icon="el-icon-plus" @click="dialogHandle('','add')">新增</el-button>
       <el-button size="small" type="primary" icon="el-icon-download" @click="exportExcel()">下载</el-button>
      </template>
   </FilterTable>
    <Drawer
      :visible.sync="visible"
      title="合同备案登记"
      :width="950"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
     >
      <addDialog  v-model="initData" @close="dialogClose" />
    </Drawer>
    <!-- 经销商弹窗 -->
   <BaseDialog title="经销商查询" :visible.sync="visibleDealer"
      width="1000px"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}" >
      <dealer-madal  ref="addDialog" @close="closeHandler" />
   </BaseDialog>
   <!-- 买受人（乙方）信息弹窗 -->
    <BaseDialog
      title="买受人（乙方）选择"
      width="1000px"
      destroyOnClose
      :visible.sync="visibleProject"
      :containerStyle="{height: '390px', overflow: 'auto', paddingBottom: '60px'}" >
      <project-madal  ref="projectDialog" :projectStatus="this.approvalManageType.PROJECT_STATUS_CHIEF_INSPECTOR_REVIEW" selectType='single'  @close="closeProject" />
   </BaseDialog>
   <!-- <Drawer  :visible.sync="visibleDrawer"
      title="合同备案登记维护"
      :width="920"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <info-modal  v-model="infoId" @close="infoClose" />
   </Drawer> -->
  </div>
</template>
<script>
import dealerMadal from '../../clue/areaBossPush/dealerMadal'
import projectMadal from '@/pages/cms-pages/customerManage/approvalManage/inquire/contractRegisterSelectList'
// import infoModal from './info'
import addDialog from './addDialog'
import * as approvalManageType from '@/api/cms-api/customerManage/approvalManage/types';
 import { exportExcelFile } from '@/utils';
import { dictionary } from '@/utils/dictMixin';
import { PHONE_PATTERN } from '@/utils/validate' // 验证
import { searchContract ,exportExcel} from '@/api/cms-api/customerManage/filingManage/contractRegister';
export default {
  name: 'customerAdd',
  mixins: [dictionary],
  components: {
    addDialog,dealerMadal,projectMadal
  },
  data() {
    return {
      visible: false,
      visibleDrawer:false,
      visibleDealer:false,
      isSelectColumn:false,
      // infoId:null,
      initData: {},
      topFilterInit:this.topFilterInitFun(),
      flterValue:{},  //参数
      fetchParams:{},  //参数
      columns:this.creatHeader(),
      fetchApi: searchContract,
      visibleProject: false,
      projectId:"",
      approvalManageType:approvalManageType,
    };
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
      //搜索数据
      topFilterInitFun(){
        return [
            { type: 'INPUT',   label: '买受人(乙方)', fieldName: 'buyerName', placeholder: '请输入',initialValue: '',
              unitRender: () => {
                return <el-button slot="append" icon="el-icon-search" onClick={() => this.projectHandle()}></el-button>;
              }
            },
            { type: 'INPUT',   label: '合同名称', fieldName: 'contractName', placeholder: '请输入',initialValue: '' },
            { type: 'SELECT',  label: '交车类型',  fieldName: 'transportType',  placeholder: '请选择', initialValue: "", itemList:  this.createDictList('2525') },
            { type: 'INPUT',   label: '项目名称', fieldName: 'projectName', placeholder: '请输入',initialValue: '' },
            { type: 'RANGE_DATE',
              label: '合同生效日期',
              fieldName: 'contractStartDate',
              valueFormat: 'yyyy-MM-dd',
              // initialValue: [] ,
              placeholder: '请选择'
            },
        ]
      },
      //搜索获取数据
      changeHandle(val){
          // this.fetchParams=Object.assign({},val);

        if (!val) {
          val = {contractStartDate:[]}
        }
        this.fetchParams = Object.assign({
          "projectId":this.projectId,
          contractStartDateStart:val.contractStartDate[0]||undefined,
          contractStartDateEnd:val.contractStartDate[1]||undefined,
        }, val);
      },
      //table表头和数据源
      creatHeader(){
         return [
         {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
         },
         {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.dialogHandle(props.row,'edit')}>
                  编辑
                </el-button>
               <el-button size="mini" type="text" onClick={() => this.dialogHandle(props.row,'show')}>明细</el-button>
              </div>
            );
          }
        },
        { title: '买受人（乙方）',  dataIndex: 'buyerName', sorter: true},
        { title: '项目名称',  dataIndex: 'projectName', sorter: true},
        { title: '合同名称',      dataIndex: 'contractName', sorter: true},
        { title: '合同编号',   dataIndex: 'contractNum', sorter: true},
        { title: '资金类型',    dataIndex: 'accoutType',  sorter: true,
           render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.accoutType, '2569')}</span>;
          }},
        { title: '交车类型',    dataIndex: 'transportType',  sorter: true,
           render: props => {    //编码转汉字
            return <span>{this.createDictText(props.row.transportType, '2525')}</span>;
          }},
        { title: '合同生效日期',  dataIndex: 'contractStartDate',dateFormat:'yyyy-MM-dd', sorter: true},
        { title: '收车地址',  dataIndex: 'receiveAddress',   sorter: true },
        { title: '合同金额',  dataIndex: 'contractPrice',  sorter: true,numberFormat:true,},
        { title: '订购总数量',  dataIndex: 'totalNum', sorter: true}
        ]
      },
      // 经销商打开窗口 -传数据
      dealerHandle(){
        this.visibleDealer=true
      },
      // 经销商关闭窗口 -返回数据
      closeHandler(data){
        //返回数据逻辑自己写
        this.topFilterInit[6].initialValue='选择经销商'
        this.visibleDealer=data.visible //关闭弹窗
    },
   // 买受人（乙方）信息 打开窗口
    projectHandle() {
      this.initPorjectData={
        excludeBigCustomer:true,//是否查询大客户经销商，如果查询 true，不查询 false
        id:1001
      }
      this.visibleProject = true;
    },
    // 买受人（乙方）信息 关闭窗口 -返回数据
    closeProject(data) {
      if(data&&data.data&&data.data.length>0){
        this.topFilterInit[0].initialValue  = data.data[0].customerName;  //买受人（乙方）
        this.projectId = data.data[0].projectId;    //项目ID
      }
      this.visibleProject = data.visible; //关闭弹窗
    },
    //详情
    dialogHandle(row,type){
      this.visible = true;
      const pram = {
        type: type,
        rowData: row
      };
      this.initData=pram;
    },
    //关闭详情
    dialogClose(data){
      this.visible=false;
      if (data.type === 'refresh') {
        // 刷新列表数据
        this.fetchParams = { ...this.fetchParams };
      }
    },
     collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    //导出
    async exportExcel(){

      // const res = await exportExcel(JSON.stringify(this.fetchParams));

      this.$refs.topFilterRef.SUBMIT_FORM();
      exportExcel(this.fetchParams).then(res => {
          exportExcelFile(res.data,"大客户合同备案登记");
      });
    }
  }
};
</script>

<style lang="less" scoped>
</style>
